package com.chenxingnet.service.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.chenxingnet.dao.CarsMapper;
import com.chenxingnet.dto.Cars;
import com.chenxingnet.service.CarsService;
import com.chenxingnet.tools.MobilePage;
import com.chenxingnet.tools.Page;
@Service("carsService")
public class CarsServiceImpl implements CarsService{
	
	@Resource
	private CarsMapper carsMapper;
	
	public int insertCars(Cars cars) {
		return carsMapper.insertSelective(cars);
		
	}

	public int updateCars(Cars cars) {
		return carsMapper.updateByPrimaryKeySelective(cars);
		
	}

	public int deleteCars(Integer cid) {
		return carsMapper.updateById(cid);
		
	}

	public Cars selectById(Integer cid) {
		
		return carsMapper.selectByPrimaryKey(cid);
	}

	
	public void getCount() {
	
		carsMapper.getCount();
	}

	
	public void selectAllByCount(HttpServletRequest request, Model model) {
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		List<Cars> carList = new ArrayList<Cars>();
		String soucar = request.getParameter("soucar");
		if(soucar != null){
			//按条件搜索
			List<Cars> carsList = carsMapper.souCars(soucar);
			model.addAttribute("carList", carsList);
		}else{
			//分页查询
			int totalCount = (int)carsMapper.getCount();
			if(pageNow != null){
				page = new Page(totalCount, Integer.valueOf(pageNow));
				carList = this.carsMapper.selectAllByCount(page.getStartPos(), page.getPageSize());
			}else{
				page = new Page(totalCount, 1);
				carList = this.carsMapper.selectAllByCount(page.getStartPos(), page.getPageSize());
			
			}
			model.addAttribute("carList", carList);
			model.addAttribute("page", page);
			
			request.setAttribute("carList", carList);
			request.setAttribute("page", page);
			
		}
		
		
	}

	public List<Cars> getAllCars() {
		return carsMapper.getAllCars();
	}

	public List<Cars> selectByCommon(String comCity) {
		
		return carsMapper.selectByCommon(comCity);
	}

	public List<Cars> getCarsByChufadi(String chufadi) {
		List<Cars>    allCarsRes=  new   ArrayList<Cars>(); 
		List<Cars>    allCars=  	carsMapper.getCarsByChufadi(chufadi);
		for(int   i=0;i<allCars.size();i++){
				if(chufadi.contains(allCars.get(i).getCcommence())){
				allCarsRes.add(allCars.get(i));
			}
		}
		return   allCarsRes;
	}

	public List<Cars> getChexingnByDeparture(String chufadi, String mudi) {
		return carsMapper.getChexingnByDeparture(chufadi,mudi);
	}

	public List<Cars> getQushuByDeparture(String chufadi, String mudi,String chexing) {
		// TODO Auto-generated method stub
		return carsMapper.getQushuByDeparture(chufadi,mudi,chexing);
	}

	public List<Cars> getChangjiaByDeparture(String chufadi, String mudi,
			String chexing, String qushu) {
		return carsMapper.getChangjiaByDeparture( chufadi,  mudi, chexing,  qushu);
	}

	public List<Cars> getJiashiJiesuanByDeparture(String chufadi, String mudi,
			String chexing, String qushu, String changjiajiesuan) {
		return carsMapper.getJiashiJiesuanByDeparture( chufadi,  mudi, chexing,  qushu,  changjiajiesuan);
	}

	/**
	 * 移动端分页查询
	 */
	public void mobSelectAllByCount(HttpServletRequest request) {
		String pageNow = request.getParameter("pageNow");
		MobilePage mobilePage = null;
		List<Cars> carList = new ArrayList<Cars>();
		String soucar = request.getParameter("soucar");
		if(soucar != null){
			//按条件搜索
			List<Cars> carsList = carsMapper.souCars(soucar);
			request.setAttribute("carList", carsList);
		}else{
			//分页查询
			int totalCount = (int)carsMapper.getCount();
			if(pageNow != null){
				mobilePage = new MobilePage(totalCount, Integer.valueOf(pageNow));
				carList = this.carsMapper.selectAllByCount(mobilePage.getStartPos(), mobilePage.getPageSize());
			}else{
				mobilePage = new MobilePage(totalCount, 1);
				carList = this.carsMapper.selectAllByCount(mobilePage.getStartPos(), mobilePage.getPageSize());
			
			}
			request.setAttribute("carList", carList);
			request.setAttribute("mobilePage", mobilePage);
			
		}
	}

	

	

	

}
